import javazoom.jl.player.*;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.decoder.SampleBuffer;

import java.io.InputStream;

public class MyPlayer extends Player{

    private boolean isPlaying;
    private InputStream stream;

    public String currentFile;

    public MyPlayer(InputStream stream) throws JavaLayerException
    {
	super(stream);	
	isPlaying = false;
    }

    public void play() throws JavaLayerException
    {
    	play(Integer.MAX_VALUE);
    }

    /**
     * Plays a number of MPEG audio frames. 
     * 
     * @param frames	The number of frames to play. 
     * @return	true if the last frame was played, or false if there are
     *			more frames. 
     */
    public boolean play(int frames) throws JavaLayerException
    {
	boolean ret = true;
	
	//This next bit of code has been modified form the original play method to check for the status of the isPlaying variable to allow for pausing functianlity		
	while (frames-- > 0 && ret)
	    {
		if(this.isPlaying)
		    ret = decodeFrame();
		else {
		    try {
			Thread.sleep(100);
		    }
		    catch(Exception e) {}			
		}
	    }
		
	if (!ret)
	    {
		// last frame, ensure all data flushed to the audio device. 
		AudioDevice out = audio;
		if (out!=null)
		    {				
			out.flush();
			synchronized (this)
			    {
				complete = (!closed);
				close();
			    }				
		    }
	    }
	return ret;
    }

    private void resume() {
	isPlaying = true;
    }

    private void pause() {
	isPlaying = false;
    }

}